package com.example.dawin_gom_test;

import android.app.Activity;
import android.content.res.Configuration;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.method.KeyListener;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.VideoView;

import com.incross.dawin.DawinConfig;
import com.incross.dawin.views.DawinAdPlayer;


public class MainActivity extends Activity implements DawinAdPlayer.OnPlayFinishListener, DawinAdPlayer.OnErrorListener, OnPreparedListener, OnCompletionListener {
	
	private String TAG = getClass().getName();
	
	private DawinAdPlayer mDawinPlayer;
	private Button mPlayButton;
	private LinearLayout mListLayout;
	private VideoView mVideoView;
	
	private LinearLayout mBackgroundLayout;
	
	private final int mRepeatCount = 1; // 반복하고싶은 재생 수(다시 호출하는 예를 구현하기위해 작성했습니다.)
	private int mPlayCount = 0;
	
	/**
	 * Activity life cycles
	 */
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.d(TAG, "onCreate()");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
	}
	
	@Override
	public void onResume() {
		Log.d(TAG, "onResume()");
		mDawinPlayer.onResume();
		super.onResume();
		
	}
	
	@Override
	public void onPause() {
		Log.d(TAG, "onPause");
		mDawinPlayer.onPause();
		super.onPause();
		
	}
	
	@Override
	public void onDestroy() {
		Log.d(TAG, "onDestroy()");
		mDawinPlayer.onDestroy();
		super.onDestroy();
		
	}
	
	@Override
	public void onBackPressed() {
		Toast.makeText(this, "onBackPressed Caled", Toast.LENGTH_SHORT).show();
		Log.d(TAG, "onBackPressed");
		mDawinPlayer.onBackPressed();
		super.onBackPressed();
		
	}
	
	@Override
	public void onConfigurationChanged (Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		Log.d(TAG, "onConfiguration changed : " + ((newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)? "Landscape" : "Portrait"));
		if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
			mListLayout.setVisibility(View.VISIBLE);
		} else {
			mListLayout.setVisibility(View.GONE);
		}
	}
	
	/**
	 * End Activity life cycles
	 */
	
	public void initView() {
		mDawinPlayer = (DawinAdPlayer) findViewById(R.id.dawin_player);
		mDawinPlayer.setOnPlayFinishListener(this);
		mDawinPlayer.setOnErrorListener(this);
		mDawinPlayer.setDawinServerType(DawinConfig.DAWIN_SERVER_TEST);
		
		mPlayButton = (Button) findViewById(R.id.btn_ad_start);
		mPlayButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				playDawin();
			}
		});
		
		mListLayout = (LinearLayout) findViewById(R.id.ll_list_area);
		mBackgroundLayout = (LinearLayout) findViewById(R.id.ll_back);
		mBackgroundLayout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Toast.makeText(MainActivity.this, "Layout click!!", Toast.LENGTH_SHORT).show();
			}
		});
		
//		mVideoView = (VideoView) findViewById(R.id.vv_video);
//		mVideoView.setOnPreparedListener(this);
//		mVideoView.setOnCompletionListener(this);
		
		
		
	}

	public void playDawin() {
//		try {
//			Thread.sleep(300);
//		} catch(InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		String gcd = "01040100";
		String mcd = "168";
		String pcd = "27";
		String scd = "02020202";
		String ccd = "09";
		
		Log.i(TAG, "Start dawin(gcd:" + gcd + ", mcd:" + mcd + ", pcd:" + pcd + ", scd:" + scd + ", ccd:" + ccd + ")");
		
		mDawinPlayer.showAd(gcd, mcd, pcd, scd, ccd);
	}
	
	public void playOtherVideo() {
		mVideoView.setVideoURI(Uri.parse("http://api.wecandeo.com/video?k=BOKNS9AQWrG9RDvOp5FmmiiBAMv9XVCsVrJZDPYhRd081OnJKBepx5Qieie"));
		mVideoView.start();
	}
	
	/**
	 * DawinAdPlayer callback methods
	 */
	@Override
	public void onError(MediaPlayer mp, int what, int extra) {
		// TODO Auto-generated method stub
		// Media player 오류를 그대로 전달할 때 사용합니다.
	}

	@Override
	public void onPlayFinished(DawinAdPlayer dawinPlayer, int finishedBy) {
		// TODO Auto-generated method stub
		Log.d(TAG, "onPlayFinished : " + finishedBy);

		switch(finishedBy) {
			case DawinAdPlayer.AD_FINISH_ACTION_BUTTON_CLICK:
				Toast.makeText(this, "액션 버튼 클릭", Toast.LENGTH_SHORT).show();
				Log.i(TAG, "액션버튼 클릭");
				break;
				
			case DawinAdPlayer.AD_FINISH_CALL_ACTION:
				Log.i(TAG, "전화 액션 종료");
				Toast.makeText(this, "전화액션 종료", Toast.LENGTH_SHORT).show();
				mPlayCount++;
				if(mRepeatCount > mPlayCount) {
					Log.i(TAG, "=================================================================");
					Log.i(TAG, "Repeat count : " + mRepeatCount + ", Current play : " + mPlayCount);
					Log.i(TAG, "=================================================================");
					playDawin();
				} else {
					mPlayCount = 0;
				}
				break;
				
			case DawinAdPlayer.AD_FINISH_ACTION_COMPLETE:
				Toast.makeText(this, "광고 재생 완료 후 종료", Toast.LENGTH_SHORT).show();
				Log.e(TAG, "광고 재생 완료 후 종료");
				mPlayCount++;
				if(mRepeatCount > mPlayCount) {
					Log.i(TAG, "=================================================================");
					Log.i(TAG, "Repeat count : " + mRepeatCount + ", Current play : " + mPlayCount);
					Log.i(TAG, "=================================================================");
					playDawin();
				} else {
					mPlayCount = 0;
				}
				break;

			case DawinAdPlayer.AD_FINISH_ACTION_SKIP:
				Toast.makeText(this, "스킵 종료", Toast.LENGTH_SHORT).show();
				Log.i(TAG, "스킵 종료");
				mPlayCount++;
				if(mRepeatCount > mPlayCount) {
					Log.i(TAG, "=================================================================");
					Log.i(TAG, "Repeat count : " + mRepeatCount + ", Current play : " + mPlayCount);
					Log.i(TAG, "=================================================================");
					playDawin();
				} else {
					mPlayCount = 0;
				}
				break;

			case DawinAdPlayer.AD_FINISH_NO_APPLICABLE_AD:
				Toast.makeText(this, "해당광고 없음 종료", Toast.LENGTH_SHORT).show();
				Log.e(TAG, "해당광고 없음 종료");
				mPlayCount = 0;
				break;
				
			case DawinAdPlayer.AD_FINISH_NETWORK_DISCONNECTED:
				Toast.makeText(this, "네트워크 불량 종료", Toast.LENGTH_SHORT).show();
				Log.e(TAG, "네트워크 불량 종료");
				mPlayCount = 0;
				break;
				
			case DawinAdPlayer.AD_FINISH_AD_INFO_RECEIVE_FAIL:
				Toast.makeText(this, "광고정보 수신 실패 종료", Toast.LENGTH_SHORT).show();
				Log.e(TAG, "광고정보 수신 실패 종료");
				mPlayCount = 0;
				break;
				
			case DawinAdPlayer.AD_FINISH_OTHER_ERROR:
				Toast.makeText(this, "기타 에러로 종료", Toast.LENGTH_SHORT).show();
				Log.e(TAG, "기타 에러로 종료");
				mPlayCount = 0;
				break;
				
			case DawinAdPlayer.AD_FINISH_AFTER_ACTION:
				Toast.makeText(this, "액션 동작 후- 종료", Toast.LENGTH_SHORT).show();
				Log.i(TAG, "액션 동작 후- 종료");
				mPlayCount++;
				if(mRepeatCount > mPlayCount) {
					Log.i(TAG, "=================================================================");
					Log.i(TAG, "Repeat count : " + mRepeatCount + ", Current play : " + mPlayCount);
					Log.i(TAG, "=================================================================");
					playDawin();
				} else {
					mPlayCount = 0;
				}
				break;
				
			case DawinAdPlayer.AD_FINISH_OTHER_REASON:
				Toast.makeText(this, "기타이유 종료", Toast.LENGTH_SHORT).show();
				Log.e(TAG, "기타이유 종료");
				break;
				
			case DawinAdPlayer.AD_FINISH_BY_SNS_SHARE:
				Toast.makeText(this, "Sns share button click 으로 인한 종료", Toast.LENGTH_SHORT).show();
				break;
				
			case DawinAdPlayer.AD_FINISH_AFTER_SNS_SHARE:
				Toast.makeText(this, "Sns share button click으로 발생한 이벤트 후 종료", Toast.LENGTH_SHORT).show();
				break;
				
			case DawinAdPlayer.AD_FINISH_BY_TICKER:
				Toast.makeText(this, "ticker button click 으로 인한 종료", Toast.LENGTH_SHORT).show();
				break;
				
			case DawinAdPlayer.AD_FINISH_AFTER_TICKER:
				Toast.makeText(this, "ticker button click으로 발생한 이벤트 후 종료", Toast.LENGTH_SHORT).show();
				break;
		}
	}

	
	
	
	
	@Override
	public void onPrepared(MediaPlayer mp) {
		Log.w(TAG, "House ad start!!");
		mp.start();
		
	}

	@Override
	public void onCompletion(MediaPlayer arg0) {
		// TODO Auto-generated method stub
		Log.w(TAG, "House ad play completed");
	}
	
	
	// 곰TV 디버깅용 key listener
	
	@Override
	public boolean onKeyUp (int keyCode, KeyEvent event) {
		if(event.getAction() == KeyEvent.KEYCODE_BACK) {
			Log.e(getClass().getName(), "Back key code!!");
		}
		Toast.makeText(this, "onKeyUp : " + keyCode, Toast.LENGTH_SHORT).show();
		
		return super.onKeyUp(keyCode, event);
	}

}
